$(function () {

    //首先加载所有笔记根据用户ID
    var user_id = getCookie("userId");
    if (user_id != null) {
        $.ajax({
            url: "/note/book/findByName.action",
            data: {"user_id": user_id},
            type: "get",
            dataType: "json",
            success: function (result) {
                //history.go(0);
                for (let i = 0; i < result.data.length; i++) {
                    let li = "";
                    let title = result.data[i].book_title;
                    li += "<li class='online'> <a > <i class='fa fa-book' title='online' rel='tooltip-bottom'> </i>" + title + "</a></li>";
                    let $li = $(li);
                    //绑定笔记本ID
                    $li.data("book_id", result.data[i].book_id);
                    $("#book_id").append($li);
                }
            },
            error: function () {

            }

        })
        //如果id为空代表没有登录，重新登陆
    } else {
        window.location.href = "log_in.html";
    }

    //动态绑定，根据选择的笔记本显示笔记
    $("#book_id").on("click", "li", function () {

        //关闭回收站笔记
        $("#pc_part_4").hide();
        //打开全部笔记
        $("#pc_part_2").show();

        $("#note_noteId").empty();
        $("#book_id a").removeClass("checked");

        var id = $(this).data("book_id");
        $("#notebookId").data("book_id", id);
        $(this).find("a").addClass("checked");


        $.ajax({
            url: "/note/noteBook/findAllNote.action",
            //1代表没有在回收站的笔记
            data: {"book_id": id, "note_status": 1},
            type: "get",
            dataType: "json",
            success: function (result) {

                // console.log(result.data.length);
                for (let i = 0; i < result.data.length; i++) {
                    let li = "";
                    let title = result.data[i].note_title;
                    li += "<li  class='online'> <a > <i class='fa fa-file-text-o' title='online' rel='tooltip-bottom'></i> " + title + " <button type='button' class='btn btn-default btn-xs btn_position btn_slide_down'><i class='fa fa-chevron-down'></i></button> </a> <div class='note_menu' tabindex='-1'> <dl> <dt> <button type='button' class='btn btn-default btn-xs btn_move'title='移动至...'><i class='fa fa-random'></i></button> </dt> <dt> <button type='button' class='btn btn-default btn-xs btn_share' title='分享'><iclass='fa fa-sitemap'></i></button> </dt> <dt> <button type='button' class='btn btn-default btn-xs btn_delete' title='删除'> <i class='fa fa-times'></i></button> </dt> </dl> </div> </li>";
                    let $li = $(li);
                    $li.data("note_id", result.data[i].note_id);
                    //$li.data("book_id", result.data[i].book_id);
                    $("#note_noteId").append($li);
                }
            },
            error: function () {

            }
        })


    })


    //创建笔记本
    $("#add_notebook").click(function () {

        $(".opacity_bg").show();
        //显示创建弹窗
        $("#can").load("alert/alert_notebook.html");

    })

    //绑定动态事件，单击创建按钮
    $("#can").on("click", ".addBook_sure", function () {

        let name = $("#input_notebook").val().trim();
        let user_id = getCookie("userId");
        if (name == "") {
            $("#addBook_span").html("笔记本名字不能为空");

        } else {
            $.ajax({
                url: "/note/book/createBook.action",
                data: {"user_id": user_id, "book_title": name},
                type: "get",
                dataType: "json",
                success: function (result) {
                    let li = "";
                    let title = name;
                    li += "<li class='online'> <a > <i class='fa fa-book' title='online' rel='tooltip-bottom'> </i>" + title + "</a></li>";
                    let $li = $(li);
                    //新建笔记本时添加ID
                    $li.data("book_id", result.data.book_id);
                    $("#book_id").append($li);
                    $("#can").html("");
                    $(".opacity_bg").hide();


                },
                error: function () {

                }

            })


        }

    })


})